<!DOCTYPE html>
<html lang="zh-CN" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="UTF-8">
    <title>文件系统-上传文件</title>
    <link rel="stylesheet" type="text/css" href="../css/font-awesome.min.css">
    <link rel="stylesheet" type="text/css" href="../css/bootstrap.min.css"/>
    <link rel="stylesheet" type="text/css" href="../css/history.css"/>
    <script th:src="@{../js/jquery-3.4.1.min.js}"></script>
    <script th:src="@{../js/jquery.cookie.js}"></script>
    <script th:src="@{../js/jquery.fileDownload.js}"></script>
</head>
<body>
<nav class="navbar navbar-default">
    <div class="container-fluid">
        <!-- Brand and toggle get grouped for better mobile display -->
        <div class="navbar-header">
            <button type="button" class="navbar-toggle collapsed" data-toggle="collapse"
                    data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
                <span class="sr-only">Toggle navigation</span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </button>
            <a class="navbar-brand active" href="#">系统功能 : </a>
        </div>
        <!-- Collect the nav links, forms, and other content for toggling -->
        <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
            <ul class="nav navbar-nav">
                <li class="active"><a href="#">首页<span class="sr-only">(current)</span></a></li>
                <li><a href="/file/history">历史记录</a></li>
            </ul>
        </div><!-- /.navbar-collapse -->
    </div><!-- /.container-fluid -->
</nav>
<div style="text-align: center;">
    <form id="uploadfile" name="uploadfile" method="post" class="row"
          style="width: 30%;text-align: center;display: inline-block" enctype="multipart/form-data">
        <div class="form-group">
            <div class="input-group">
                <input id='location' class="form-control" readonly required>
                <label class="input-group-btn">
                    <input type="button" id="i-check" value="选择文件" class="btn btn-primary"
                           onclick="$('#file').click();">
                </label>
            </div>
            <input type="file" name="file" id='file' accept="*.*" onchange="$('#location').val($('#file').val());"
                   style="display: none">
        </div>
        <button type="button" class="btn btn-primary" id="btn-submit" style="margin-top: 2%;">上传</button>
    </form>
</div>

<table class="table table-bordered" style="width: 80%" align="center" hidden id="file_table">
    <caption>上传记录：</caption>
    <thead>
    <tr>
        <th>原始文件名</th>
        <th>现在文件名</th>
        <th>文件大小</th>
        <th>文件类型</th>
        <th>上传时间</th>
        <th>操作</th>
    </tr>
    </thead>
    <tbody>
    <tr class="success">
        <td id="file_name"></td>
        <td id="file_now_name"></td>
        <td id="file_size"></td>
        <td id="file_type"></td>
        <td id="file_upload_time"></td>
        <td id="file_download">
                <button type="button" class="btn btn-primary" id="down_btn" uuid="123">下载</button>
        </td>
    </tr>
    </tbody>
</table>
</body>
<script>
    var $ = $.noConflict();
    var file = $('#file');

    $(document).ready(function () {
        console.log("1 : " + $.cookie('data'));
        initList();
    });

    function initList() {
        try {
            /*var data = JSON.parse($.session.get('data'));
            console.log("2 : " + $.session.get('data'));*/
            var data = JSON.parse($.cookie('data'));
            console.log("data : " + $.cookie('data'));
            $('#file_table').removeAttr('hidden');
            $('#file_name').html(data.FileName);
            $('#file_now_name').html(data.UUID_name);
            $('#file_size').html(data.size);
            $('#file_type').html(data.type);
            $('#file_upload_time').html(data.Create_time);
            $('#down_btn').attr('uuid', data.UUID);

        } catch (e) {
            console.log(e);
        }
    }

    function upload() {
        var formdata = new FormData(document.getElementById("uploadfile"));
        $.ajax({
            url: "/file/upload.do",
            type: "POST",
            data: formdata,
            async: false,
            cache: false,
            contentType: false,
            processData: false,
            success: function (req) {
                if (req.status == 200) {
                    var data = req.data;
                    $.cookie('data' , JSON.stringify(data));
                    //$.session.set('data', JSON.stringify(data));
                    $('#location').val("");
                    file.val("");
                    initList();
                    alert("上传成功！");
                } else if(req.status == 410 || req.status == 403){
                    alert(req.msg);
                } else {
                    alert("未知错误！");
                }
                $('#btn-submit').removeAttr("disabled");
                $('#btn-submit').text('上传');
            }, error: function (request) {
                alert("上传失败！");
                $('#btn-submit').removeAttr("disabled");
                $('#btn-submit').text('上传');
            }
        });
    }

    $('#btn-submit').click(function () {
        var path = file.val();
        if (path != "" && path != undefined && path.length > 0) {
            $('#btn-submit').attr('disabled', "true");
            $('#btn-submit').text('上传中...');
            setTimeout(upload, 1000);
        } else {
            alert("请选择文件");
        }
    });

    $('#down_btn').click(function () {
        var url = "/file/download.do?uuid=" + $('#file_now_name').html();
        $.fileDownload(url, {
            httpMethod: 'GET',
            successCallback: function (url) {
                console.log(url);
            },
            failCallback: function (req, url) {
                var reg = /<pre.+?>(.+)<\/pre>/g;
                req.match(reg);
                req = RegExp.$1;
                var json = JSON.parse(req);
                alert(json['json']);
            }
        });
    });
</script>
</html>